#!/bin/bash
declare -A testSets
testSets["arrayAccess"]="./ts-swift-workload/array-access/ts/"
testSets["fannkuc-redux"]="./ts-swift-workload/array-access/ts/"
testSets["fastarepeat"]="./ts-swift-workload/array-access/ts/"
testSets["fastarandom1"]="./ts-swift-workload/array-access/ts/"
testSets["fastarandom2"]="./ts-swift-workload/array-access/ts/"
testSets["mandelbrot"]="./ts-swift-workload/array-access/ts/"
testSets["spectralnorm"]="./ts-swift-workload/array-access/ts/"
testSets["threeDCube"]="./ts-swift-workload/array-access/ts/"
testSets["threeDRaytrace"]="./ts-swift-workload/array-access/ts/"
testSets["Method_Call"]="./ts-swift-workload/function-call/ts/"
testSets["Normal_Call"]="./ts-swift-workload/function-call/ts/"
testSets["Pointer_Call"]="./ts-swift-workload/function-call/ts/"
testSets["StaticFunc_Call"]="./ts-swift-workload/function-call/ts/"
testSets["nbody"]="./ts-swift-workload/nbody/ts/"
testSets["cocos"]="./ts-swift-workload/cocos/ts/"
testSets["bitops"]="./ts-swift-workload/numerical-calculation/ts/"
testSets["cordic"]="./ts-swift-workload/numerical-calculation/ts/"
testSets["numericalComputation"]="./ts-swift-workload/numerical-calculation/ts/"
testSets["binarySearchFloat"]="./ts-swift-workload/numerical-calculation/ts/"
testSets["binarySearchInteger"]="./ts-swift-workload/numerical-calculation/ts/"
testSets["partialSum"]="./ts-swift-workload/numerical-calculation/ts/"
testSets["propertyaccess2"]="./ts-swift-workload/property-access/ts/"
testSets["splay"]="./ts-swift-workload/splay/ts/"

sdk_path="../.."
ark_js_vm="ark_js_vm"
toolspath=./toolspath.txt
tools_type="dev"

if [ -s "$toolspath" ]; then
    while IFS= read -r line; do
        case $line in
            *--tools-path\ *)
                sdk_path="${line#*--tools-path\ }"
                ;;
            *--ark_js_vm\ *)
                ark_js_vm="${line#*--ark_js_vm\ }"
                ;;
            *--es2abc\ *)
                es2abc="${line#*--es2abc\ }"
                ;;
            *--ark_aot_compiler\ *)
                ark_aot_compiler="${line#*--ark_aot_compiler\ }"
                ;;
            *--tools-type\ *)
                tools_type="${line#*--tools-type\ }"
                ;;
        esac
    done < "toolspath.txt"
fi

arg_name=""
true="true"
false="false"
run_count=1
aarch64=""
date=$(date +'%Y%m%d%H%M%S')

function help_explain(){
    echo "[--help,--h]"
    echo "[--run-count <count>]"
    echo "[--case <case-name>]"
    echo "[--date <date>]"
    echo "[--build]"
    echo "[--excel]"
}
if [ -z "$1" ]; then
    echo "No input provided."
else
    for arg in "$@"; do
        case $arg_name in
            "--case")
                arg_name=""
                case_name=$arg
                ;;
            "--date")
                arg_name=""
                date=$arg
                ;;
            "--run-count")
                arg_name=""
                run_count=$arg
                ;;
        esac
        case $arg in
            "--h")
                help_explain
                exit
                ;;
            "--help")
                help_explain
                exit
                ;;
            "--build")
                build="true"
                ;;
            "--case")
                arg_name=$arg
                ;;
            "--date")
                arg_name=$arg
                ;;
            "--excel")
                excel="true"
                ;;
            "--run-count")
                arg_name=$arg
                ;;
            "--aarch64")
                aarch64="--compiler-target-triple=aarch64-unknown-linux-gnu "
                ;;
        esac
    done
fi
case $tools_type in
    "dev")
        export LD_LIBRARY_PATH=${sdk_path}/out/x64.release/lib.unstripped/arkcompiler/ets_runtime:${sdk_path}/out/x64.release/lib.unstripped/thirdparty/icu:${sdk_path}/prebuilts/clang/ohos/linux-x86_64/llvm/lib:${sdk_path}/out/x64.release/lib.unstripped/thirdparty/zlib
        ;;
    "rk3568")
        export LD_LIBRARY_PATH=${sdk_path}/out/rk3568/clang_x64/arkcompiler/ets_runtime:${sdk_path}/out/hispark_taurus/clang_x64/thirdparty/icu:${sdk_path}/out/hispark_taurus/clang_x64/thirdparty/zlib:${sdk_path}/prebuilts/clang/ohos/linux-x86_64/llvm/lib
        ;;
    "hispark_taurus")
        export LD_LIBRARY_PATH=${sdk_path}/out/hispark_taurus/clang_x64/arkcompiler/ets_runtime:${sdk_path}/out/rk3568/clang_x64/thirdparty/icu:${sdk_path}/out/rk3568/clang_x64/thirdparty/zlib:${sdk_path}/out/hispark_taurus/clang_x64/thirdparty/cjson:${sdk_path}/prebuilts/clang/ohos/linux-x86_64/llvm/lib
        ;;
esac
pgo2path="pgo_build/${date}"
$es2abc $sdk_path/arkcompiler/ets_runtime/ecmascript/ts_types/lib_ark_builtins.d.ts --type-extractor --type-dts-builtin --module --merge-abc --extension=ts --output $sdk_path/arkcompiler/ets_runtime/ecmascript/ts_types/lib_ark_builtins.d.abc
function build_pgo(){
    local ts_file=$1$2.ts
    local file_name=$2
    local abc_file=${pgo2path}/${file_name}.abc
    echo build ${file_name}
    ${es2abc} ${ts_file} --type-extractor --module --merge-abc --extension=ts --output ${pgo2path}/${file_name}.abc
    ${ark_js_vm} --log-level=info --enable-pgo-profiler=true --compiler-opt-inlining=true --compiler-pgo-profiler-path=./${pgo2path}/${file_name}.ap --log-level=info --asm-interpreter=true --entry-point=${file_name} ${abc_file}
    ${ark_aot_compiler} --log-level=info --enable-pgo-profiler=true --compiler-opt-loop-peeling=true --compiler-fast-compile=false --compiler-opt-inlining=true --compiler-max-inline-bytecodes=45 --compiler-opt-level=2 --builtins-dts=${sdk_path}/arkcompiler/ets_runtime/ecmascript/ts_types/lib_ark_builtins.d.abc --compiler-pgo-profiler-path=./${pgo2path}/${file_name}.ap ${aarch64} --aot-file=./${pgo2path}/${file_name} ${abc_file}
}

function run_pgo(){
    local file_name=$1
    local abc_file=${pgo2path}/${file_name}.abc
    echo run ${file_name}
    ${ark_js_vm} --entry-point=${file_name} --aot-file=./${pgo2path}/${file_name} ${abc_file}
}


if [[ "$build" = "true" ]]; then
    if [ ! -d $pgo2path ];then
        mkdir -p $pgo2path
    fi
    if [ -z "$case_name" ]; then
        for key in ${!testSets[@]}; do
            if [ -n "${testSets[${key}]}" ]; then
                build_pgo ${testSets[${key}]} ${key} 
            else
                echo "${key} does't exist in cases"
            fi
        done
    else
        if [ -n "${testSets[${case_name}]}" ]; then
            build_pgo ${testSets[${case_name}]} ${case_name}
        else
            echo "${case_name} does't exist in cases"
        fi
    fi
fi

if [ ! -d $pgo2path ];then
    echo "${pgo2path} No such file or directory"
    exit
fi

if [[ "$excel" = "true" ]]; then
    if [ -z "$case_name" ]; then
        for key in ${!testSets[@]}; do
            python pgo2excel.py --case-dir $pgo2path --run-case $key --run-count $run_count --ark_js_vm $ark_js_vm
        done
    else
        python pgo2excel.py --case-dir $pgo2path --run-case $case_name --run-count $run_count --ark_js_vm $ark_js_vm
    fi
fi
